Page({
  data: {
    curId:'',
    name:'',
    sex:'',
    birthday:'',
    allergyAttr:['皮肤','胃肠道','呼吸道','无'],
    allergyIndex:'',
    milk:'',
    allergy:'',
  },
  onLoad(e) {

    if(e.id){

      this.setData({
        curId:e.id
      })
      wx.request({
        url: getApp().globalData.apiBaseUrl + 'baby/view',
        data:{
          id:this.data.curId
        },
        method: 'get',
        success:  (res)=> {
          console.log(res,122)
          if (res.data.code == 200) {
  
            this.setData({
              name:res.data.data.name,
              sex:res.data.data.sex,
              birthday:res.data.data.birthday,
              milk:res.data.data.milk,
              allergy:res.data.data.allergy,
            })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000 //持续的时间
            })
          }
        }
      })
    }
  },
  changeSex(e){
    this.setData({
      sex:e.currentTarget.dataset.sex
    })
  },
  formSubmit(e){
    console.log(e,12122)
    let api=getApp().globalData.apiBaseUrl + 'baby/add'
    let post={
      userId:1,
      userName:'测试用户',
      sex:this.data.sex,
      birthday:this.data.birthday,
      name:e.detail.value.name,
      allergy:e.detail.value.allergy,
      milk:e.detail.value.milk,
    }
    if(this.data.curId){
      post.id=this.data.curId
      api=getApp().globalData.apiBaseUrl + 'baby/update'
    }
    wx.request({
      url: api,
      data:post,
      method: 'post',
      success:  (res)=> {
        console.log(res,122)
        if (res.data.code == 200) {
          
          wx.showToast({
            title: '提交成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          setTimeout(()=>{

            wx.navigateTo({
              url: '/pages/index/index',
            })
          },1500)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
    // wx.navigateTo({
    //   url: '/pages/index/index',
    // })
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      birthday: e.detail.value
    })
  },
  bindPickerChange1: function(e) {
    this.setData({
      skuIndex:e.detail.value,
    })
  },
});
